<template>
  <div class="container">
    <div class="export-header">
      <el-button  icon="el-icon-edit" type="primary" size='medium' @click="addBtn">新增</el-button>
    </div>
    <div class="input_modal">
      <div class="input-item">
        <span>商品分类名：</span>
        <el-input v-model="categoryName" placeholder="请输入内容" class="proName" @keyup.enter.native="search"></el-input>
        <el-button icon="el-icon-search" type="primary" size="medium" @click="getList">查询</el-button>
      </div>
    </div>
    <div class="table_con">
      <el-table v-loading="loading" :data="tableData" :border='true'>
        <el-table-column :formatter="formatter" prop="categoryName" label="商品分类"></el-table-column>
        <el-table-column prop="" label="操作">
          <template slot-scope="scope">
            <el-button @click="editBtn(scope.row)" type="primary" size="mini">编辑</el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" @click="deleteRole(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="pageIndex" :page-size="pageSize" :page-sizes='[10,20,30,40]' layout="total, sizes, prev, pager, next, jumper" :total="pageCount">
      </el-pagination>
    </div>
    <el-dialog title="添加/编辑分类" :visible.sync="dialogVisible" width="30%">
      <div class="tag-head">
        <el-form :model="user"  ref="user" label-width="120px" class="">
          <el-form-item label="分类名" prop="categoryName">
            <el-input v-model="user.categoryName" placeholder="请输入"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handSave('user')" type="primary">确 定</el-button>
        <el-button style="margin-right:30%" type="primary" @click="dialogVisible = false">返 回</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
let CONSTANT = require("../../constant/constant.js");
let common = require("../../common.js");
export default {
  data() {
    return {
      tableData: [],
      dialogVisible:false,
      pageCount: null,
      pageIndex: 1,
      pageSize: 10,
      loading: true,
      user:{
          id:'',
          categoryName:''
      },
      categoryName:'',
    };
  },
  mounted() {
    this.pageIndex = this.$router.currentRoute.query.pageIndex
      ? this.$router.currentRoute.query.pageIndex
      : 1;
    this.getList();
  },
  methods: {
    formatter(row, column, cellValue) {
      return cellValue ? cellValue : "- -";
    },
    addBtn(){
        this.user = {
            id:'',
            categoryName:''
        }
        this.dialogVisible = true
    },  
    editBtn(obj){
        this.user = {
            id:obj.id,
            categoryName:obj.categoryName
        }
        this.dialogVisible = true
    },
    deleteRole:function(id) {
        this.$confirm('确定删除?', '提示', {           
            confirmButtonText: '确定',           
            cancelButtonText: '取消',           
            type: 'warning'         
        }).then(() => {           
            let  url = CONSTANT.URL.LIST.deleteEsCategory;
            let param = {id:id} ;
            common.requestAjax(url,JSON.stringify(param),null,(res)=>{
                if(res.status == 200){
                    this.$message({
                        type:'success',
                        message:'删除成功!'
                    });
                    this.getList();
                }
                else{
                    this.$message({
                        type:'warning',
                        message:res.msg
                    })
                }
            })      
        }).catch(() => {           
            this.$message({             
                type: 'info',             
                message: '已取消删除'           
            });                   
        });    
    },
    handSave(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let url = CONSTANT.URL.LIST.saveEsCategory;
          common.requestAjax(url, JSON.stringify(this.user), null, res => {
            if (res.status == 200) {
              this.$message({
                type: "success",
                message: "保存成功！"
              });
              this.getList();
              this.dialogVisible = false;
              this.user = {
                id: "",
                papersName: "",
                status: "normal"
              };
               this.$router.replace({
                  name: "testAdd",
                  query: { pageIndex: this.pageIndex, id:res.data.bussData.id }
                });
            } else {
              this.$message({
                type: "warning",
                message: res.msg
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getList: function() {
      let url = CONSTANT.URL.LIST.findEsCategoryByPage;
      let data = {
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        categoryName:this.categoryName
      };
      this.loading = true;
      common.requestAjax(url, JSON.stringify(data), null, res => {
        this.loading = false;
        let data = res.data;
        this.tableData = data.bussData;
        this.pageCount = data.count;
      });
    },
    search: function(e) {
      if (e.keyCode == 13) {
        this.pageIndex = 1;
        this.getList();
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getList();
    }
  }
};
</script>
